/**
 * @Title: ToastView.java
 * @author 7828wc
 * @date 2016年1月21日 下午7:52:34
 */
package com.onstar.cn.weight;

import android.content.Context;
import android.graphics.Paint;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.lang.reflect.Field;

/**
 * @author 7828wc
 */
public class ToastView extends Toast {
    private TextView tv_center_text;
    private Context context;
    private View toast_id;
    private View view;

    /**
     * @param context
     */
    public ToastView(Context context) {
        super(context);
        this.context = context;
    }

    public void showRemoteToast(String text) {
        view = LayoutInflater.from(context).inflate(
                R.layout.weight_toast_view, null);
        tv_center_text = (TextView) view.findViewById(R.id.tv_center_text);
        tv_center_text.setText(text);
        this.setView(view);
        this.setDuration(Toast.LENGTH_SHORT);
        this.setGravity(Gravity.TOP, 0, 0);
        this.show();
    }

    public void showNetToast(int resId) {
        view = LayoutInflater.from(context).inflate(
                R.layout.weight_net_view, null);
        tv_center_text = (TextView) view.findViewById(R.id.des_tv);
        tv_center_text.setText(resId);
        changeToastWindowParams(this);
        this.setView(view);
        this.setDuration(Toast.LENGTH_LONG);
        this.setGravity(Gravity.TOP | Gravity.FILL_HORIZONTAL, 0, 0);
        this.show();
    }

    public void showUnAuthorizedToast(int backId,int resId) {
        view = LayoutInflater.from(context).inflate(
                R.layout.weight_net_view, null);
        view.setBackgroundResource(backId);
        tv_center_text = (TextView) view.findViewById(R.id.des_tv);
        ImageView mJumpIb = (ImageView) view.findViewById(R.id.jump_ib);
        mJumpIb.setVisibility(View.GONE);
        tv_center_text.setText(resId);
        this.setView(view);
        this.setDuration(Toast.LENGTH_LONG);
        this.setGravity(Gravity.TOP | Gravity.FILL_HORIZONTAL, 0, 0);
        this.show();
    }

    public void showAuthorizedToast(int backId,int titleId,int desId) {
        view = LayoutInflater.from(context).inflate(
                R.layout.weight_auth_view, null);
        view.setBackgroundResource(backId);
        tv_center_text = (TextView) view.findViewById(R.id.des_tv);
        tv_center_text.setText(desId);
        TextView titleTv = (TextView) view.findViewById(R.id.title_tv);
        titleTv.setText(titleId);
        this.setView(view);
        this.setDuration(Toast.LENGTH_LONG);
        this.setGravity(Gravity.TOP | Gravity.FILL_HORIZONTAL, 0, 0);
        this.show();
    }

    public void showFootToast(int resId, boolean showArrow) {
        showNetToast(resId);
        view.findViewById(R.id.jump_ib).setVisibility(showArrow ? View.VISIBLE : View.GONE);
    }

    //改方法让toast可以点击
    private void changeToastWindowParams(Toast toast) {
        try {
            Field field = Toast.class.getDeclaredField("mTN");
            if (field != null) {
                try {
                    field.setAccessible(true);
                    Field fieldParams = field.get(toast).getClass().getDeclaredField("mParams");
                    fieldParams.setAccessible(true);
                    WindowManager.LayoutParams lp = (WindowManager.LayoutParams) fieldParams.get(field.get(toast));
                    lp.flags = WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON |
                            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void showNetToastWithClickListen(int resId, View.OnClickListener listener) {
        showNetToast(resId);
        if (view != null && listener != null) {
            RelativeLayout rl = (RelativeLayout) view.findViewById(R.id.toast_id);
            rl.setOnClickListener(listener);
        }

    }

    public void showToast(int resId) {
        TextView view = (TextView) LayoutInflater.from(context).inflate(
                R.layout.toast_view, null);
        this.view = view;
        view.setText(resId);
        this.setView(view);
        this.setDuration(Toast.LENGTH_SHORT);
        this.setGravity(Gravity.CENTER, 0, 0);
        this.show();
    }

    public void showToast(String text) {
        TextView view = (TextView) LayoutInflater.from(context).inflate(
                R.layout.toast_view, null);
        this.view = view;
        view.setText(text);
        this.setView(view);
        this.setDuration(Toast.LENGTH_SHORT);
        this.setGravity(Gravity.CENTER, 0, 0);
        this.show();
    }

    /**
     * 用于有发送失败按钮情况
     *
     * @param msg
     * @param clickListener
     */
    public void toast(String msg, View.OnClickListener clickListener) {
        view = LayoutInflater.from(context).inflate(
                R.layout.weight_toast_view, null);
        toast_id = view.findViewById(R.id.toast_id);
        TextView again_send = (TextView) view.findViewById(R.id.again_send);
        TextView tv_center_text = (TextView) view.findViewById(R.id.tv_center_text);
        tv_center_text.setText(msg);
        again_send.setOnClickListener(clickListener);
        again_send.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG); //下划线
        changeToastWindowParams(this);
        this.setView(view);
        this.setDuration(Toast.LENGTH_LONG);
        this.setGravity(Gravity.TOP | Gravity.FILL_HORIZONTAL, 0, 0);
        this.show();
    }

    public void toastLongX2(String msg, View.OnClickListener clickListener) {
        toast(msg, clickListener);
        toast(msg, clickListener);
    }


    public void setToastClickListener(View.OnClickListener clickListener) {
        changeToastWindowParams(this);
        view = LayoutInflater.from(context).inflate(
                R.layout.weight_toast_view, null);
        toast_id = view.findViewById(R.id.toast_id);
        TextView again_send = (TextView) view.findViewById(R.id.again_send);
        again_send.setOnClickListener(clickListener);
    }

    public void toast(String text) {
        Toast toast = Toast.makeText(context, text, Toast.LENGTH_LONG);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.show();
    }

    public void toastShort(String text) {
        Toast toast = Toast.makeText(context, text, Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.show();
    }

    public void toast(int resId) {
        Toast toast = Toast.makeText(context, resId, Toast.LENGTH_LONG);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.show();
    }

    public void toastShort(int resId) {
        Toast toast = Toast.makeText(context, resId, Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.show();
    }
}
